package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.*;
import csu.web.mypetstore.persistence.CartDao;
import csu.web.mypetstore.persistence.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private static final String CLEAR_CART = "DELETE FROM cart;";
    private static final String INCREMENT_QUANTITY = "UPDATE cart " +
            "SET quantity = quantity + 1 " +
            "WHERE username = ? AND itemid = ?;";

    private static final String SET_QUANTITY = "UPDATE cart " +
            "SET quantity = ? " +
            "WHERE username = ? AND itemid = ?;";
    private  static  final String GET_CART_ITEM="SELECT * FROM cart WHERE username = ? AND itemid = ?;";
    private static final String GET_CART_ITEMS_BY_USERNAME="SELECT itemid, username, quantity FROM cart WHERE username = ?;";

    private static final String ADD_ITEM = "INSERT INTO cart (itemid, username, quantity) VALUES (?, ?, ?);";
    private static final String REMOVE_ITEM = "DELETE FROM cart " +
            "WHERE username = ? AND itemid = ?;";
    private static final String IS_IN_CART = "SELECT * FROM cart " +
            "WHERE username = ? AND itemid = ?";
    @Override
    public CartItem getCartItem(String username,String itemId) {
        CartItem cartItem = new CartItem();
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_CART_ITEM);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,itemId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                cartItem.setItemId(resultSet.getString(1));
                cartItem.setUsername(resultSet.getString(2));
                cartItem.setQuantity(resultSet.getInt(3));
                cartItem.setInStock(true);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cartItem;
    }

    @Override
    public List<CartItem> getItemsListByUsername(String username){
        List<CartItem> cartItemList = new ArrayList<>();
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_CART_ITEMS_BY_USERNAME);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                CartItem cartItem = new CartItem();
                cartItem.setItemId(resultSet.getString(1));
                cartItem.setUsername(resultSet.getString(2));
                cartItem.setQuantity(resultSet.getInt(3));
                cartItem.setInStock(true);
                cartItemList.add(cartItem);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return cartItemList;
    }

    @Override
    public void addItem(CartItem cartItem) {
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(ADD_ITEM);
            preparedStatement.setString(1,cartItem.getItemId());
            preparedStatement.setString(2,cartItem.getUserName());
            preparedStatement.setInt(3,cartItem.getQuantity());
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void incrementQuantity(String username,String itemId) {
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INCREMENT_QUANTITY);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,itemId);

            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void setQuantity(String username,String itemId, int quantity) {
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(SET_QUANTITY);
            preparedStatement.setInt(1,quantity);
            preparedStatement.setString(2,username);
            preparedStatement.setString(3,itemId);

            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void removeItem(String username,String itemId) {
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(REMOVE_ITEM);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,itemId);

            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean isInCart(String username,String itemId) {
        boolean flag = false;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(IS_IN_CART);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, itemId);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                flag = true;
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    @Override
    public void clearCart(){
        try{
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(CLEAR_CART);

            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
